<?php
	class Carreras extends Zend_Db_Table_Abstract
	{
		protected $_primary = 'id_gp';
		protected $_name = 'carrera';
		
		/* Instancia de la Tabla  @var unknown_type  */
        protected static $_instance = null;
		
		/* Obtener la instancia de la tabla de manera estatica  @return Zend_Db_Table */
        public static function getInstance()
        {
            if (null === self::$_instance) 
			{
                self::$_instance = new self();
            }
            return self::$_instance;
        }
		
		/* SELECT id_piloto_pole, count(*), (SELECT COUNT(*) FROM carrera) as n_carreras FROM carrera GROUP BY id_piloto_pole */
		public static function getPoleStats()
		{
			$table = self::getInstance();
			$select = $table->select();
			$select->setIntegrityCheck(false);
			//$select->from($table,array('COUNT(*) as num'));
			$select->from
			(
				$table,array
				(
					'num'	=>  new Zend_Db_Expr('COUNT(*)'),
					'total' => new Zend_Db_Expr('(SELECT COUNT(*) FROM carrera)')
				)
			);
			$select->joinLeft('piloto','piloto.id_piloto = carrera.id_piloto_pole',
				array 
				(
					'piloto_pole' 	=> 'piloto.nombre_piloto',
					'escuderia_pole'=> 'piloto.escuderia'
				)
			);
			$select->group('id_piloto_pole');
			return $table->fetchAll($select);
		}
		
		public static function getPuestoStats($i)
		{
			$table = self::getInstance();
			$select = $table->select();
			$select->setIntegrityCheck(false);
			$select->from
			(
				$table,array
				(
					'num'	=>  new Zend_Db_Expr('COUNT(*)'),
					'total' => new Zend_Db_Expr('(SELECT COUNT(*) FROM carrera)')
				)
			);
			$select->joinLeft('piloto','piloto.id_piloto = carrera.id_piloto_p'.$i,
				array 
				(
					'piloto' 	=> 'piloto.nombre_piloto',
					'escuderia_pole'=> 'piloto.escuderia'
				)
			);
			$select->group('carrera.id_piloto_p'.$i);
			return $table->fetchAll($select);
		}
	}